import request from '@/utils/request.ts'
import type { PagesData, ResponseData } from '@/api/type.ts'
import type { SKU } from '@/api/product/sku/type.ts'

enum API {
  SKU_ADD_URL = '/admin/product/saveSkuInfo',
  SKU_FIND_BY_SPU_URL = '/admin/product/findBySpuId',
  SKU_LIST_URL = '/admin/product/list',
  SKU_ON_SALE_URL = '/admin/product/onSale',
  SKU_CANCEL_SALE_URL = '/admin/product/cancelSale',
  SKU_INFO_URL = '/admin/product/getSkuInfo',
  SKU_DELETE_URL = '/admin/product/deleteSku',
}

export const reqAddSku = (sku: SKU) => {
  return request.post<any, ResponseData<any>>(API.SKU_ADD_URL, sku)
}

export const reqSkuBySpuId = (spuId: number) => {
  return request.get<any, ResponseData<SKU[]>>(
    `${API.SKU_FIND_BY_SPU_URL}/${spuId}`,
  )
}

export const reqSkuList = (page: number, limit: number) => {
  return request.get<any, ResponseData<PagesData<SKU>>>(
    `${API.SKU_LIST_URL}/${page}/${limit}`,
  )
}

// 切换sku上下架状态
export const reqSkuToggleSale = (id: number, toggle: boolean) => {
  if (toggle) {
    return request.get<any, ResponseData<any>>(`${API.SKU_ON_SALE_URL}/${id}`)
  } else {
    return request.get<any, ResponseData<any>>(
      `${API.SKU_CANCEL_SALE_URL}/${id}`,
    )
  }
}

export const reqSkuInfo = (skuId: number) => {
  return request.get<any, ResponseData<SKU>>(`${API.SKU_INFO_URL}/${skuId}`)
}

export const reqSkuDelete = (skuId: number) => {
  return request.delete<any, ResponseData<any>>(
    `${API.SKU_DELETE_URL}/${skuId}`,
  )
}
